package com.back.demo.controller;

import com.back.demo.common.CommonResult;
import com.back.demo.controller.myVO.StudentVO.ChangeStudentInfoReqVO;
import com.back.demo.controller.myVO.StudentVO.*;
import com.back.demo.common.StudentInfo;
import com.back.demo.service.StudentService;
import com.back.demo.util.JwtTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@Api(tags = "学生功能")
@RestController
@RequestMapping("/back/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;


    // 查询学生个人信息表
    @ApiOperation("学生个人信息查询接口")
    @GetMapping("/get-info")
    public CommonResult<?> getStudentInfo(@RequestHeader("Authorization") String authHeader) {

        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        // 返回学生个人信息
        StudentInfo studentInfo = studentService.findStudentInfoByStudentId(studentId);
        return CommonResult.success(studentInfo);
    }

    // 查找学生个人总结信息
    @ApiOperation("学生个人总结查询接口")
    @GetMapping("/get-summary")
    public CommonResult<?> getStudentSummary(@RequestHeader("Authorization") String authHeader) {

        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        // 返回个人总结
        String summary = studentService.findStudentSummaryByStudentId(studentId);
        return CommonResult.success(summary);
    }


    // 插入学生个人总结信息
    @ApiOperation("学生个人总结提交接口")
    @PostMapping("/add-summary")
    public CommonResult<?> addStudentSummary(@RequestHeader("Authorization") String authHeader, @RequestBody StudentSummaryReqVO studentSummaryReqVO) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        // 将学生个人总结保存到数据库，并处理插入失败的情况
        try {
            studentService.addStudentSummary(studentId, studentSummaryReqVO.getSummary());
        } catch (Exception e) {
            System.out.println(e.toString());
            return CommonResult.error(50003, "提交失败");
        }
        return CommonResult.success("提交成功");
    }


    // 查找学生科研信息
    @ApiOperation("学生科研查询接口")
    @GetMapping("/get-research")
    public CommonResult<?> getStudentResearch(@RequestHeader("Authorization") String authHeader) {

        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        // 返回科研信息
        ArrayList<StudentResearchVO> listVO = studentService.findStudentResearchByStudentId(studentId);
        return CommonResult.success(listVO);
    }

    // 插入学生科研信息，只处理新增和修改的情况，不处理删除的情况
    @ApiOperation("学生科研提交接口")
    @PostMapping("/add-research")
    public CommonResult<?> addStudentResearch(@RequestHeader("Authorization") String authHeader, @RequestBody StudentResearchVO reqVO) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);
        if (reqVO.getId() == 0) { // 表示新增，并处理插入失败的情况
            try {
                studentService.addStudentResearchByStudentId(studentId, reqVO);
            } catch (Exception e) {
                System.out.println(e.toString());
                return CommonResult.error(50003, "提交失败");
            }
        } else { // 表示修改，并处理修改失败的情况
            try {
                studentService.modifyStudentResearchById(reqVO);
            } catch (Exception e) {
                System.out.println(e.toString());
                return CommonResult.error(50003, "提交失败");
            }
        }
        return CommonResult.success("提交成功");
    }

    // 删除学生科研信息
    @ApiOperation("学生科研删除接口")
    @PostMapping("/delete-research")
    public CommonResult<?> deleteStudentResearch(@RequestHeader("Authorization") String authHeader, @RequestBody DeleteReqVO reqVO) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        try {
            studentService.deleteStudentResearchById(reqVO.getId());
        } catch (Exception e) {
            System.out.println(e.toString());
            return CommonResult.error(50003, "删除失败");
        }

        return CommonResult.success("删除成功");
    }


    //查找学生社会实践信息
    @ApiOperation("学生社会实践查询接口")
    @GetMapping("/get-practice")
    public CommonResult<?> getStudentPractice(@RequestHeader("Authorization") String authHeader) {

        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        // 返回社会实践信息
        ArrayList<StudentPracticeVO> listVO = studentService.findStudentPracticeByStudentId(studentId);
        return CommonResult.success(listVO);
    }

    // 插入学生社会实践信息
    @ApiOperation("学生社会实践提交接口")
    @PostMapping("/add-practice")
    public CommonResult<?> addStudentPracticeList(@RequestHeader("Authorization") String authHeader, @RequestBody StudentPracticeVO reqVO) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);
        if (reqVO.getId() == 0) { // 表示新增，并处理插入失败的情况
            try {
                studentService.addStudentPracticeByStudentId(studentId, reqVO);
            } catch (Exception e) {
                System.out.println(e.toString());
                return CommonResult.error(50003, "提交失败");
            }
        } else { // 表示修改，并处理修改失败的情况
            try {
                studentService.modifyStudentPracticeById(reqVO);
            } catch (Exception e) {
                System.out.println(e.toString());
                return CommonResult.error(50003, "提交失败");
            }
        }
        return CommonResult.success("提交成功");

    }

    // 删除学生社会实践信息
    @ApiOperation("学生社会实践删除接口")
    @PostMapping("/delete-practice")
    public CommonResult<?> deleteStudentPractice(@RequestHeader("Authorization") String authHeader, @RequestBody DeleteReqVO reqVO) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        try {
            studentService.deleteStudentPracticeById(reqVO.getId());
        } catch (Exception e) {
            System.out.println(e.toString());
            return CommonResult.error(50003, "删除失败");
        }

        return CommonResult.success("删除成功");
    }


    //查找学生骨干服务岗位信息
    @ApiOperation("学生骨干服务岗位查询接口")
    @GetMapping("/get-position")
    public CommonResult<?> getStudentPosition(@RequestHeader("Authorization") String authHeader) {

        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        // 返回骨干服务岗位
        ArrayList<StudentPositionVO> listVO = studentService.findStudentPositionByStudentId(studentId);
        return CommonResult.success(listVO);
    }

    // 插入学生骨干服务岗位信息
    @ApiOperation("学生骨干服务岗位提交接口")
    @PostMapping("/add-position")
    public CommonResult<?> addStudentPosition(@RequestHeader("Authorization") String authHeader, @RequestBody StudentPositionVO reqVO) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);
        if (reqVO.getId() == 0) { // 表示新增，并处理插入失败的情况
            try {
                studentService.addStudentPositionByStudentId(studentId, reqVO);
            } catch (Exception e) {
                System.out.println(e.toString());
                return CommonResult.error(50003, "提交失败");
            }
        } else { // 表示修改，并处理修改失败的情况
            try {
                studentService.modifyStudentPositionById(reqVO);
            } catch (Exception e) {
                System.out.println(e.toString());
                return CommonResult.error(50003, "提交失败");
            }
        }
        return CommonResult.success("提交成功");

    }

    // 删除学生骨干服务岗位信息
    @ApiOperation("学生骨干服务岗位删除接口")
    @PostMapping("/delete-position")
    public CommonResult<?> deleteStudentPosition(@RequestHeader("Authorization") String authHeader, @RequestBody DeleteReqVO reqVO) {
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        try {
            studentService.deleteStudentPositionById(reqVO.getId());
        } catch (Exception e) {
            System.out.println(e.toString());
            return CommonResult.error(50003, "删除失败");
        }

        return CommonResult.success("删除成功");
    }


    @ApiOperation("学生修改联系方式")
    @PostMapping("/change-info")
    public CommonResult<?> changeInformation(@RequestHeader("Authorization") String authHeader, @RequestBody ChangeStudentInfoReqVO reqVO) {
        String token = authHeader.substring(7);
        String studentId = jwtTokenUtil.getUsernameFromToken(token);

        StudentInfo studentInfo = studentService.findStudentInfoByStudentId(studentId);
        if (studentInfo == null) {
            return CommonResult.error(50003, "用户不存在，修改个人信息失败");
        }

        try {
            // 执行修改个人信息的操作
            studentService.changeStudentInfo(studentId, reqVO.getPhone(), reqVO.getEmail());
        } catch (Exception e) {
            // 处理修改失败的情况
            System.out.println(e.toString());
            return CommonResult.error(50003, "修改个人信息失败");
        }
        return CommonResult.success("修改个人信息成功");
    }


}
